//-------------------------------------------
// game.cpp
//-------------------------------------------

/**
 * @file game.cpp
 * Contains the implemnetation for the class that has the game information
 *
 * @see logic::CGame
 */

#include "game.h"
#include <assert.h>

namespace logic {

	CGame::CGame() : playing(false)/*, player1(NULL), player2(NULL) */{}
	CGame::~CGame() {}

	bool CGame::init() {

		// Init the physics engine
		//CPhysicsEngine::getInstance().init();

		// Create the player
		//player1 = new logic::CPlayer(this);
		//entities.push_back(player1);
		//this->emitPlayerCreated(player1);
		//player1->setPosition(CPosition(0.0, 10.0, 0.0));

		// Create the scene
		// CPhysicsEngine::getInstance().createScene();

		playing = true;

		return true;

	} // init

	void CGame::end()
	{

		// Remove all the entities
		//std::list<CEntity *>::iterator it, itEnd;
		//itEnd = entities.end();

		//for (it = entities.begin(); it != itEnd; ++it)
		//	delete (*it);

		//entities.clear();
		//player1 = NULL;
		//player2 = NULL;

		// Remove the commands and events
		commandQueue.clear();
		eventQueue.end();

		playing = false;

		// End the physics engine
		//CPhysicsEngine::getInstance().end();

	} // end

	void CGame::update()
	{

		assert(playing && "Game has not began!");

		// 1. Process the command queue
		this->processCommandQueue();
		commandQueue.erase(commandQueue.begin(), commandQueue.end());

		// 2. Decide each avatar action
		//player1->actuate();
		//player2->actuate();

		// 3. Simulate the action
		//CPhysicsEngine::getInstance().simulate(dt);

		// 4. Update the entities
		//std::list<CEntity*>iterator it, itEnd;
		//itEnd = entities.end();
		//for (it = entities.begin(); it != itEnd; ++it)
		//	(*it)->update();

		// 5. Process the event queue
		this->processEventQueue();
		eventQueue.erase(eventQueue.begin(), eventQueue.end());

	}

	void CGame::insertCommand(TCommand command)
	{

		assert(playing && "Game has not began!");
		commandQueue.push_back(command);

	}

	/****************************************************************************/
	/*		METHODS FOR THE INSERTION OF EVENTS FROM THE REST OF THE LOGIC      */
	/****************************************************************************/

	void CGame::clean()
	{
		assert(playing && "Game has not began!");
	}

	void CGame::processCommandQueue()
	{
		assert(playing && "Game has not began!");

		std::vector<TCommand>::iterator it, itEnd;
		itEnd = commandQueue.end();

		for (it = commandQueue.begin(); it != itEnd; ++it)
		{
			TCommand c = *it;
			switch (c.id)
			{
				case MOVEFWD:
					//if (c.activate)
					//{
					//	if (c.player == 1)
					//		player1->movefwd();
					//	else
					//		player2->movefwd();
					//}
					//else
					//{
					//	if (c.player == 1)
					//		player1->stop();
					//	else
					//		player2->stop();
					//}
					break;

				case MOVEBWD:
					//if (c.activate)
					//{
					//	if (c.player == 1)
					//		player1->movebwd();
					//	else
					//		player2->movebwd();
					//}
					//else
					//{
					//	if (c.player == 1)
					//		player1->stop();
					//	else
					//		player2->stop();
					//}
					break;
			} // switch
		} // for
	} // processCommandQueue

	void CGame::processEventQueue()
	{
		assert(playing && "Game has not began!");

		std::vector<TEvent>::const_iterator it, itEnd;
		itEnd = eventQueue.end();

		for (it = eventQueue.begin(); it != itEnd; ++it)
		{
			//switch (it->id)
			//{
			//	case EDEAD:
			//		this->emitPlayerDead();
			//		break;

			//} // switch
		} // for
	} // processEventQueue

	/******************************/
	/*		OBSERVER METHODS      */
	/******************************/

	//bool CGame::addObserver(CPlayer::Observer* newObserver)
	//{
	//	return this->player1->addObserver(newObserver);
	//}

	//bool CGame::removeObserver(CPlayer::Observer* oldObserver)
	//{
	//	return this->player1->removeObserver(oldObserver);
	//}

} // namespace logic